﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bzzb.Model;
using Masuit.Tools.Security;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;

namespace Bzzb.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly ILogger<UserController> _logger;
        private readonly IFreeSql _fsql;
        public UserController(ILogger<UserController> logger, IFreeSql fsql){
            this._logger = logger;
            this._fsql = fsql;
        }

        [HttpPost]
        public async Task<Dictionary<string, Object>> Login([FromForm] User loginUser, [FromForm] int isWeb = 0)
        {
            User user = await _fsql.Select<User>().Where(u => u.Username == loginUser.Username).ToOneAsync();

            if (user == null) {
                return RenderJson(status: "failed", message: "该用户不存在", code: 1001, data: user);
            }

            if (!user.Password.Equals(loginUser.Password))
            {
                return RenderJson(status: "failed", message: "密码错误", code: 1002, data: user);
            }

            if (user.Workstatus.Equals("离职"))
            {
                return RenderJson(status: "failed", message: "该账号已禁用", code: 1002, data: user);
            }

            if (isWeb == 1) {
                if (user.Role.Equals("巡检员"))
                {
                    return RenderJson(status: "failed", message: "该账号已禁用", code: 1002, data: user);
                }
            }
            //List<RenWu> renwus = await _fsql.Select<RenWu>().Where(rw => DateTime.Now >= rw.BeginTime && DateTime.Now <= rw.EndTime && rw.Duty == user.Duty).ToListAsync();
            JArray jo_renwus = new();
            //List<RenWu> renwus = await _fsql.Select<RenWu>().Where(rw => rw.Rq == DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")) && rw.Duty == user.Duty).ToListAsync();
            //_logger.LogInformation("用户任务列表" + _fsql.Select<RenWu>().Where(rw => rw.Rq == DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")) && rw.Duty == user.Duty).ToSql());
            List<RenWu> renwus = await _fsql.Select<RenWu>().Where(rw => rw.Duty == user.Duty && rw.Status != "已关闭").OrderBy("id desc").ToListAsync();
            _logger.LogInformation("用户任务列表" + _fsql.Select<RenWu>().Where(rw => rw.Duty == user.Duty && rw.Status != "已关闭").OrderBy("id desc").ToSql());
            foreach (var renwu in renwus)
            {
                JObject jo_renwu = JObject.FromObject(renwu);
                jo_renwu.Add("dq01", JArray.FromObject(await _fsql.Select<LuXian>().Where(lx => lx.Lxmc == "电气01").ToListAsync()));
                jo_renwu.Add("dq02", JArray.FromObject(await _fsql.Select<LuXian>().Where(lx => lx.Lxmc == "电气02").ToListAsync()));
                jo_renwu.Add("qj01", JArray.FromObject(await _fsql.Select<LuXian>().Where(lx => lx.Lxmc == "汽机01").ToListAsync()));
                jo_renwu.Add("qj02", JArray.FromObject(await _fsql.Select<LuXian>().Where(lx => lx.Lxmc == "汽机02").ToListAsync()));
                jo_renwu.Add("gl01", JArray.FromObject(await _fsql.Select<LuXian>().Where(lx => lx.Lxmc == "锅炉01").ToListAsync()));
                jo_renwu.Add("gl02", JArray.FromObject(await _fsql.Select<LuXian>().Where(lx => lx.Lxmc == "锅炉02").ToListAsync()));
                jo_renwus.Add(jo_renwu);
            }

            JObject data = JObject.FromObject(user);
            data.Add("tasklist", jo_renwus);

            return RenderJson(data: data);
        }

        [NonAction]
        protected Dictionary<string, Object> RenderJson(string status = "success", string message = "成功", int code = 0, Object data = null)
        {
            Dictionary<string, Object> result = new Dictionary<string, object>();
            result.Add("status", status);
            result.Add("message", message);
            result.Add("code", code);
            result.Add("data", data);
            return result;
        }
    }
}
